#ifndef TCPCONNECTION_H
#define TCPCONNECTION_H

#include <functional>
#include <memory>
#include <string>
#include <utility>
class EventLoop;
class Channel;

class TcpConnection : public std::enable_shared_from_this<TcpConnection> {
public:
    using ConnectionCallback = std::function<void(const std::shared_ptr<TcpConnection>&)>;
    using MessageCallback = std::function<void(const std::shared_ptr<TcpConnection>&, const std::string&)>;
    using CloseCallback = std::function<void(const std::shared_ptr<TcpConnection>&)>;

    TcpConnection(EventLoop* loop, int sockfd);
    TcpConnection(EventLoop* loop, int sockfd, const std::string& peerAddr);
    ~TcpConnection();

    void setConnectionCallback(ConnectionCallback cb) { connectionCallback_ = std::move(cb);}
    void setMessageCallback(MessageCallback cb) { messageCallback_ = std::move(cb);}
    void setCloseCallback(CloseCallback cb) { closeCallback_ = std::move(cb); }

    const std::string& getWriteBuffer() const { return writeBuffer_; }
    const std::string& getReadBuffer() const { return readBuffer_; }
    int getSockfd() const { return sockfd_; }

    void handleRead();
    void handleWrite();
    void handleClose();
    void send(const std::string& data);
    virtual void shutdown();

private:
    EventLoop* loop_;
    int sockfd_;
    bool closed_;
    std::string peerAddr_;
    std::shared_ptr<Channel> channel_;

    ConnectionCallback connectionCallback_;
    MessageCallback messageCallback_;
    CloseCallback closeCallback_;

    std::string readBuffer_;
    std::string writeBuffer_;
};

#endif